/*
 * Copyright (c) 2006-2021, Ulandlink Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-09-18     lfz         first version
 */
#include <rtthread.h>
#include <string.h>
#include <stdlib.h>
#include <elog.h>
#include <elog_flash.h>	

#ifdef RT_USING_FINSH

static void elog(uint8_t argc, char **argv)
{
    
#define CMD_SWITCH_INDEX      0
#define CMD_LVL_INDEX         1
#define CMD_COLOR_INDEX       2
#define CMD_FLASH_INDEX       3

    const char* help_info[] =
    {
        [CMD_SWITCH_INDEX]    = "elog switch [on/off]      - EasyLogger output enabled [on/off]",
        [CMD_LVL_INDEX]       = "elog lvl [0-5]            - EasyLogger Set filter level",
        [CMD_COLOR_INDEX]     = "elog color [on/off]       - EasyLogger color output enabled [on/off]",
        [CMD_FLASH_INDEX]     = "elog flash <cmd>          - EasyLogger <read> <clean> <flush> flash log",
    };
    if (argc < 2){
        rt_kprintf("Usage:\n");
        for (size_t i = 0; i < sizeof(help_info) / sizeof(char*); i++){
            rt_kprintf("%s\n", help_info[i]);
        }
        rt_kprintf("\n");
    
    } else {
        const char *operator = argv[1];
        if (!rt_strcmp(operator, "switch")){
            if (!rt_strcmp(argv[2], "on") || !rt_strcmp(argv[2], "ON")) {
                elog_set_output_enabled(true);
            } else if (!rt_strcmp(argv[2], "off") || !rt_strcmp(argv[2], "OFF")) {
                elog_set_output_enabled(false);
            }
        
        } else if (!rt_strcmp(operator, "lvl")){   
            if ((atoi(argv[2]) <= ELOG_LVL_VERBOSE) && (atoi(argv[2]) >= ELOG_LVL_ASSERT)) {
                elog_set_filter_lvl(atoi(argv[2]));
            }
        } 
 #ifdef ELOG_COLOR_ENABLE
        else if (!rt_strcmp(operator, "color")){
            if (!rt_strcmp(argv[2], "on") || !rt_strcmp(argv[2], "ON")) {
                elog_set_text_color_enabled(true);
            } else if (!rt_strcmp(argv[2], "off") || !rt_strcmp(argv[2], "OFF")) {
                elog_set_text_color_enabled(false);
            }
        }
#endif
        
#ifdef PKG_EASYFLASH_LOG
        else if (!rt_strcmp(operator, "flash")){
            if (!strcmp(argv[2], "read")) {
                if (argc >= 4) {
                    elog_flash_output_recent(atol(argv[3]));
                }else {
                    elog_flash_output_all();
                }
            } 
            else if (!strcmp(argv[2], "clean")) {
                elog_flash_clean();
            } 
            else if (!strcmp(argv[2], "flush")) {

                #ifdef ELOG_FLASH_USING_BUF_MODE
                    elog_flash_flush();
                #else
                    rt_kprintf("EasyLogger flash log buffer mode is not open.\n");
                #endif
            }      
        } 
#endif        
        else {   
            rt_kprintf("Unknown command. Please enter 'elog' for help.\n");
        }       
    }


}

MSH_CMD_EXPORT(elog, EasyLogger operate.);
#endif /* RT_USING_FINSH */

